<template>
  <transition name='slide'>
    <div class="user-center">
      <div class='back' @click='back'>
        <i class='icon-back'></i>
      </div>
      <div class='switches-wrapper'>
        <switches :currentIndex='currentIndex' :switches='switches' @switch='switchItem'></switches>
      </div>
      <div class='play-btn' ref='playBtn' @click='random'>
        <i class='icon-play'></i>
        <span class='text'>全部播放</span>
      </div>
      <div class='list-wrapper' ref='listWrapper'>
        <scroll ref='favoriteList' class='list-scroll' v-if='currentIndex === 0' :data='favoriteList'>
          <div class='list-inner'>
            <song-list @select='selectSong' :songs='favoriteList'></song-list>
          </div>
        </scroll>
        <scroll ref='playList' class='list-scroll' v-if='currentIndex === 1' :data='playHistory'>
          <div class='list-inner'>
            <song-list @select='selectSong' :songs='playHistory'></song-list>
          </div>
        </scroll>
      </div>
      <div class='no-result-wrapper' v-show='noResult'>
        <no-result title='没有了'></no-result>
      </div>
    </div>
  </transition>
</template>

<script>
  import Switches from 'base/switches/switches.vue'
  import SongList from 'base/song-list/song-list'
  import Scroll from 'base/scroll/scroll'
  import NoResult from 'components/no-result/no-result.vue'
  import Song from 'common/js/song'
  import { playlistMixin } from 'common/js/mixin'
  import { mapGetters, mapActions } from 'vuex'
  import { favoriteList, playHistory } from '../../store/getters'

  export default {
    mixins: [playlistMixin],
    components: {
      Switches,
      SongList,
      NoResult,
      Scroll
    },
    data() {
      return {
        currentIndex: 0,
        switches: [
          { name: '我喜欢的' },
          { name: '最近听的' }
        ]
      }
    },
    props: {},
    watch: {},
    methods: {
      handlePlaylist(newVal) {
        const bottom = newVal.length > 0 ? '60px' : '0'
        this.$refs.listWrapper.style.bottom = bottom
        this.$refs.favoriteList && this.$refs.favoriteList.refresh()
        this.$refs.playList && this.$refs.playList.refresh()
      },
      back() {
        this.$router.back()
      },
      switchItem(item) {
        this.currentIndex = item
      },
      selectSong(song) {
        this.insertSong(new Song(song))
      },
      random() {
        let list = this.currentIndex === 0 ? this.favoriteList : this.playHistory
        if (list.length === 0) {
          return
        }
        list = list.map((song) => {
          return new Song(song)
        })
        this.randomPlay({
          list: list
        })
      },
      ...mapActions([
        'insertSong',
        'randomPlay'
      ])
    },
    computed: {
      noResult() {
        if (this.currentIndex === 0) {
          return !this.favoriteList.length
        } else {
          return !this.playHistory.length
        }
      },
      ...mapGetters(
        [
          'favoriteList',
          'playHistory'
        ]
      )
    },
    created() {
    },
    mounted() {
    },
    destroyed() {
    }
  }
</script>

<style lang="stylus" scoped>
  @import "~common/stylus/variable"

  .user-center
    position: fixed
    top: 0
    bottom: 0
    z-index: 100
    width: 100%
    background: $color-background
    &.slide-enter-active, &.slide-leave-active
      transition: all 0.3s
    &.slide-enter, &.slide-leave-to
      transform: translate3d(100%, 0, 0)
    .back
      position absolute
      top: 0
      left: 6px
      z-index: 50
      .icon-back
        display: block
        padding: 10px
        font-size: $font-size-large-x
        color: $color-theme
    .switches-wrapper
      margin: 10px 0 30px 0
    .play-btn
      box-sizing: border-box
      width: 135px
      padding: 7px 0
      margin: 0 auto
      text-align: center
      border: 1px solid  $color-text-l
      color: $color-text-l
      border-radius: 100px
      font-size: 0
      .icon-play
        display: inline-block
        vertical-align: middle
        margin-right: 6px
        font-size: $font-size-medium-x
      .text
        display: inline-block
        vertical-align: middle
        font-size: $font-size-small
    .list-wrapper
      position: absolute
      top: 110px
      bottom: 0
      width: 100%
      .list-scroll
        height: 100%
        overflow: hidden
        .list-inner
          padding: 20px 30px
    .no-result-wrapper
      position: absolute
      width: 100%
      top: 50%
      transform: translateY(-50%)
</style>
